<template>
  <div class="login">
    <div class="login_title cenFlex">
      <div class="logoCss img">
        <img src="~@/assets/hlImg/logo.png" alt="" />
      </div>
      <div class="logoCss_text">海亮精准教学</div>
    </div>

    <div class="login_bg">
      <img src="~@/assets/hlImg/login/login_hl1.jpg" alt="" />
    </div>
    <div class="login_box">
      <div class="login_box_top">欢迎登录</div>
      <div class="login_box_top_sub" style="margin-bottom:30px">
        精准教学总后台
      </div>
      <div class="login_box_b">
        <div class="login_box_input">
          <div class="show_pwd">
            <img
              src="~@/assets/hlImg/login/login_hl6.png"
              v-if="!dataForm.username"
            />
            <img src="~@/assets/hlImg/login/login_hl6_a.png" v-else />
          </div>
          <input
            type="text"
            v-model="dataForm.username"
            placeholder="请输入帐号"
          />
        </div>
        <div class="login_box_input">
          <div class="show_pwd">
            <img
              src="~@/assets/hlImg/login/login_hl2.png"
              v-if="!dataForm.password"
            />
            <img src="~@/assets/hlImg/login/login_hl2_a.png" v-else />
          </div>
          <input
            ref="password"
            v-model="dataForm.password"
            placeholder="请输入密码"
            type="password"
          />
        </div>
        <div class="login_box_code sbFlex">
          <div class="login_box_code_l">
            <div class="show_pwd">
              <img
                src="~@/assets/hlImg/login/login_hl5.png"
                v-if="!dataForm.captcha"
              />
              <img src="~@/assets/hlImg/login/login_hl5_a.png" v-else />
            </div>
            <input
              type="text"
              v-model="dataForm.captcha"
              placeholder="请输入验证码"
            />
          </div>
          <div class="login_box_code_r">
            <img :src="captchaPath" @click="getCaptcha()" alt="" />
          </div>
        </div>
        <div class="login_box_btn cenFlex" @click="dataFormSubmit()">
          登录
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getUUID } from '@/utils';
import Common from '@/utils/common.js';
import { login, getOwnButton, tempPublicKey, getUserInfo } from '@/api/api.js';
export default {
  data() {
    return {
      dataForm: {
        username: '',
        password: '',
        uuid: '',
        captcha: '',
      },
      dataRule: {
        username: [
          { required: true, message: '帐号不能为空', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
        ],
        captcha: [
          { required: true, message: '验证码不能为空', trigger: 'blur' },
        ],
      },
      captchaPath: '',
    };
  },
  created() {
    this.getCaptcha();
  },
  methods: {
    // 提交表单
    dataFormSubmit() {
      this.loginFn({
        username: this.dataForm.username,
        password: this.dataForm.password,
        uuid: this.dataForm.uuid,
        captcha: this.dataForm.captcha,
      });
    },
    // 登录
    async loginFn(e) {
      console.log(e.password, this.dataForm.publicKey);
      e = {
        ...e,
        password: Common.getRsaCode(e.password, this.dataForm.publicKey),
      };
      // 接口统一管理
      let res = await login(e);
      if (res[0]) {
        this.getCaptcha();
        return;
      }
      let data = res[1];
      if (data && data.code === 0) {
        this.$cookie.set('token', data.data);

        localStorage.setItem('token', data.data);
        localStorage.setItem(
          'tokenEnd',
          data.data.expire * 1000 + new Date().getTime()
        );
        // localStorage.setItem("refreshToken",data.data.refreshToken);
        getUserInfo({}).then((res) => {
          let userInfo = res[1];
          console.log(userInfo);
          this.$cookie.set('sys_user_id', userInfo.user.userId);
          localStorage.setItem('sys_user_id', userInfo.user.userId);
          this.$nextTick(async () => {
            let res1 = await getOwnButton({});
            if (res1[0]) {
              return;
            }
            let permissions = [];
            if (res1[1].data && res1[1].data.length) {
              res1[1].data.forEach((ele) => {
                permissions.push(ele.btn_key);
              });
            }
            localStorage.setItem(
              'permissions',
              JSON.stringify(permissions || '[]')
            );
            this.$router.replace({ name: 'home' });
          });
        });
      } else {
        this.$message.error(data.msg);
      }
      this.getCaptcha();
    },
    // 获取验证码
    getCaptcha() {
      tempPublicKey().then((key) => {
        if (key[1].code == 0) {
          let { uuid, publicKey } = key[1].data;
          this.dataForm.uuid = uuid;
          this.dataForm.publicKey = publicKey;
          this.captchaPath = this.$http.adornUrl(
            `/usercenter/captcha.jpg?uuid=${uuid}`
          );
        }
      });
    },
  },
};
</script>

<style lang="scss">
div {
  box-sizing: border-box;
}

img {
  width: 100%;
}

input {
  width: 100%;
  height: 100%;
  font-size: 14px;
  border: 0px;
}
*,
*:before,
*:after {
  box-sizing: inherit;
}

* {
  margin: 0;
  padding: 0;
  outline: 0;
}

.login {
  width: 100vw;
  height: 100vh;
  overflow: hidden;
}

.login_title {
  position: absolute;
  left: 60px;
  top: 64px;
  z-index: 999;

  .logoCss {
    width: 42px;
    height: 42px;
    min-width: 42px;
    margin-right: 18px;
  }

  .logoCss_text {
    font-family: Alibaba PuHuiTi 2;
    font-weight: 900;
    color: #17233d;
    font-size: 36px;
    letter-spacing: 5px;
    line-height: 45px;
  }
}

.login_bg {
  width: 100%;
  height: 100%;
  position: fixed;
  left: 0px;
  top: 0px;

  img {
    width: 100%;
    min-height: 100vh;
  }
}

.login_box {
  width: 456px;
  height: 493px;
  position: fixed;
  right: 5vw;
  top: 50%;
  transform: translateY(-50%);
  background: #fff;
  padding: 50px 60px;
  opacity: 0.9;
  border-radius: 4px;
}

.login_box_top {
  font-family: PingFang SC;
  color: #666666;
  font-size: 18px;
  line-height: 33px;
}

.login_box_top_sub {
  font-family: PingFang SC;
  font-weight: 600;
  color: #000926;
  font-size: 26px;
  line-height: 33px;
}

.login_box_b {
  padding: 0px 13px;
}

.login_box_input {
  width: 100%;
  height: 48px;
  opacity: 1;
  padding: 15px;
  margin-bottom: 32px;
  position: relative;
  border-bottom: 1px #80909db5 solid;
  padding-left: 35px;
  font-size: 14px;
}

.show_pwd {
  position: absolute;
  left: 0px;
  top: 50%;
  width: 16px;
  height: 16px;
  transform: translateY(-50%);
}

.login_box_code {
  display: flex;
  align-items: center;
  justify-content: space-between;

  margin-bottom: 40px;

  .login_box_code_l {
    width: 196px;
    height: 48px;
    padding: 15px;
    position: relative;
    border-bottom: 1px #80909db5 solid;
    padding-left: 35px;
  }

  .login_box_code_r {
    width: 100px;
    height: 40px;
    margin-left: 40px;
    margin-right: 10px;

    img {
      width: 100%;
      height: 100%;
    }
  }
}

.login_box_btn {
  width: 100%;
  height: 48px;
  border-radius: 8px;
  background: #1b54ed;
  margin: 0px auto;
  font-family: PingFang SC;
  color: #ffffff;
  font-size: 17px;
  letter-spacing: 10px;
  line-height: normal;
  cursor: pointer;
}

.foot {
  width: 100vw;
  position: relative;
  height: 3.6vw;
  bottom: 0px;
  left: 0px;
  position: fixed;
  color: #fff;
  display: flex;
  justify-content: space-between;
  flex-direction: column;
  padding: 1vw;
  padding-bottom: 0.2vw;
  font-size: 14px;
  min-height: 76px;

  &::after {
    content: '';
    display: block;
    width: 100%;
    height: 100%;
    opacity: 0.5;
    background: rgba(255, 255, 255, 0.2);
    position: absolute;
    bottom: 0px;
    left: 0px;
    z-index: 2;
  }

  .txt {
    width: 100%;
    position: relative;
    z-index: 3;
    color: #fff;
    text-align: center;
  }
}
</style>
